<script setup lang="ts">
import { ref } from 'vue';


import { notification } from 'ant-design-vue';

import { ensureKeyLength, encrypt } from '../utils/common';
import { ResultMessage } from "../types/commonType";
import { register as userRegister } from '../api';


let password = ref("")
let email = ref("")
let username = ref("")

const secretKey = ensureKeyLength('cbhhawe123-keyword-pass-secretkey')

const register = async () => {
    try {
        if (username.value === "") {
            var config = {
                message: "注册失败",
                description: "请输入你的用户名~",
            }
            return notification.warning(config)
        }
        // 处理密码数据
        if (password.value === "" || email.value === "") {
            var config = {
                message: "注册失败",
                description: password.value === "" ? "请输入你的密码~" : "请输入你的邮箱~",
            }

            return notification.warning(config)
        }


        const passwordHash = encrypt(password.value, secretKey);

        const requestData = {
            password: passwordHash,
            username: username.value,
            email: email.value,
        }

        const postOption = {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(requestData),
        };
        const response = await userRegister(postOption) as ResultMessage;
        var config = {
            message: "注册成功",
            description: "欢迎光临~",
        }
        if (response.status !== "200") {
            config.message = "注册失败"
            config.description = response.message
            return notification.error(config)
        }
        notification.success(config)
        // 登录成功后跳转到登录页面
        window.location.href = "/login"

        // 处理登录成功的逻辑
    } catch (error: any) {
        config = {
            message: "注册失败",
            description: error.message || "未知错误",
        }
        return notification.warning(config)
    }
}
</script>

<template>
    <div class="login-container">
        <!-- 放置logo图片 -->
        <div class="logo_img">
            <img src="../assets/avatar.png" alt="logo" />
        </div>

        <form @submit.prevent="register">
            <div class="form-group">
                <label for="username">用户名：</label>
                <input v-model="username" type="text" id="username" placeholder="请输入你的用户名~" />
            </div>
            <div class="form-group">
                <label for="email">邮&emsp;箱：</label>
                <input v-model="email" type="text" id="email" placeholder="请输入你的邮箱~" />
            </div>
            <div class="form-group">
                <label for="password">密&emsp;码：</label>
                <input v-model="password" type="password" id="password" placeholder="请输入你的密码~" />
            </div>
            <div class="form-group">
                <button class="login_btn">注册</button>
            </div>
        </form>
    </div>
</template>



<style scoped>
.login-container {
    display: flex;
    user-select: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background-color: #f2e6e6;
    /* 登录页面背景颜色过渡色，唯美 */
    background-image: linear-gradient(to right, #f2e6e6, #d8c1c1);
}

.logo_img {
    width: 100px;
    height: 100px;
    margin-bottom: 20px;
    border-radius: 50%;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;

    img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        object-position: center;
        user-select: none;

        &:hover {
            transform: scale(1.05);
            transition: all 0.3s;

        }
    }
}

.login_btn {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    background-color: #4caf50;
    color: #fff;
}

.form-group {
    display: flex;
    margin-bottom: 20px;
    align-items: center;
}

label {
    width: 100px;
    height: 100%;
    user-select: none;
    display: block;
    color: #666;
}

input {
    width: 100%;
    padding: 8px;
    box-sizing: border-box;
    border: 1px solid #ccc;
    border-radius: 4px;
}

button {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    background-color: #4caf50;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

button:hover {
    background-color: #45a049;
}
</style>